package com.situ.day2;

import org.junit.Test;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Scanner;

public class ArrayDemo {
    @Test
    public void test1() {
        //数组：存放同一种类型数据的集合，在内存里面是开辟一块连续的区域。
        int[] array = new int[4];
        array[0] = 23;
        array[1] = 3;
        array[2] = 4;
        array[3] = 24;

        System.out.println(array[2]);
        System.out.println(array.length);//4
        System.out.println("------------");
        for (int i = 0; i < array.length; i++) {
            //System.out.println(i);
            System.out.println(array[i]);
        }
        System.out.println("-----------");
        for (int i = array.length -1; i >= 0; i--) {
            System.out.println(array[i]);
        }
        //array.fori
        System.out.println("-----------");
        //增强的for循环
        for (int item : array) {
            System.out.println(item);
        }
    }

    @Test
    public void test33() {
        int[] array1 = {12, 34, 34, 6};
        int[] array2 = new int[]{12, 34, 34, 6};
        for (int i = 0; i < array2.length; i++) {
            System.out.println(array2[i]);
        }
    }

    @Test
    public void test331() {
        int[] array = new int[4];
        array[0] = 23;
        array[1] = 3;
        array[2] = 4;
        array[3] = 24;
        int sum = 0;
        for (int i = 0; i < array.length; i++) {
            sum += array[i];
        }
        System.out.println("sum: " + sum);
    }

    @Test
    public void test332() {
        int[] array = new int[4];
        array[0] = 23;
        array[1] = 3;
        array[2] = 44;
        array[3] = 24;
        //假设第一个是最大的
        int max = array[0];
        for (int i = 0; i < array.length; i++) {
            if (array[i] > max) {
                max = array[i];
            }
        }
        System.out.println("max: " + max);
    }

    @Test
    public void test35() {
        int[] array = new int[4];
        array[0] = 23;
        array[1] = 3;
        array[2] = 44;
        array[3] = 24;
        //实参：实际传过去参数
        int max = getMax(array);
        System.out.println("max: " + max);
    }

    //方法签名  形参
    public int getMax(int[] array) {//方法体
        int max = array[0];
        for (int i = 0; i < array.length; i++) {
            if (array[i] > max) {
                max = array[i];
            }
        }
        return max;
    }

    @Test
    public void test335() {
        int[] arr = {40, 17, 21, 1};
        sort(arr);
        for (int i = 0; i < arr.length; i++) {
            System.out.println(arr[i]);
        }
    }

    //参数   array = arr
    public void sort(int[] array) {
        //i=1   j=3
        //i=2   j=2
        //i=3   j=1
        //i+j=array.length
        //j=array.length-i
        for (int i = 1; i <= array.length - 1; i++) {
            //i代表比较多少趟
            for (int j = 0; j < array.length- i; j++) {
                if (array[j] > array[j + 1]) {
                    int temp = array[j];
                    array[j] = array[j + 1];
                    array[j + 1] = temp;
                }
            }
        }
    }

    @Test
    public void test555() {
        int num1 = 3;
        int num2 = 5;
        int temp = num1;
        num1 = num2;
        num2 = temp;
        System.out.println("num1: " + num1);
        System.out.println("num2: " + num2);
    }

    @Test
    public void test78() {
        //int 0
        int[] source = {1, 2, 3, 4, 5};
        int[] dest = new int[10]; //destination
        System.arraycopy(source, 0, dest, 0, source.length);
        for (int i = 0; i < dest.length; i++) {
            System.out.println(dest[i]);
        }

        int[] array = Arrays.copyOf(source, 10);
        System.out.println(array);
    }

    @Test
    public void test89() {
        int[][] array = new int[3][];
        array[0] = new int[2];
        array[1] = new int[2];
        array[2] = new int[3];
        array[0][0] = 23;
        array[0][1] = 12;
        array[1][0] = 22;
        array[1][1] = 22;
        array[2][0] = 21;
        array[2][1] = 22;
        array[2][2] = 23;
        //int[][] array = {{1,2},{2,3},{3,4,5}};
        System.out.println(array.length);
        for (int i = 0; i < array.length; i++) {
            //System.out.println(Arrays.toString(array[i]));
            for (int j = 0; j < array[i].length; j++) {
                System.out.println(array[i][j]);
            }
        }
    }

    @Test
    public void test781() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入字符串：");
        String str = scanner.nextLine();
        //"dsfhi 34**DSD [34"
        char[] array = str.toCharArray();
        int letterCount = 0;
        int spaceCount = 0;
        int digitCount = 0;
        int otherCount = 0;
        for (char ch : array) {
            if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) {
                letterCount++;
            } else if (ch >= '0' && ch <= '9') {
                digitCount++;
            } else if (ch == ' ') {//""
                spaceCount++;
            } else {
                otherCount++;
            }
        }



    }
}
